package model;

import java.util.*;

import Main.Mysql;


public class DocumentXML {
  
	private String name;
	private int id;
	private static Mysql bdd;
	
	public DocumentXML () {}
	
	public DocumentXML(int id, String name) {
		this.name = name;	
		this.id = id;
		bdd = Mysql.getInstance();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public List<Noeud> getNoeuds() {
		String req = "SELECT N.id, N.nomBalise, N.ordre " +
					 "FROM Documents D, Noeuds N, DocNoeud DN " +
					 "WHERE D.id =" + id + " " +
					 "AND N.id = DN.idNoeud;";

		List<List> result = bdd.getResultSet (bdd.executeSelect(req));
		List<Noeud> toReturn = new ArrayList<Noeud>();
		for (int i=0;i<result.size();i++){
			Noeud n = new Noeud(Integer.parseInt((String) result.get(i).get(0)), (String)result.get(i).get(1), Integer.parseInt((String) result.get(i).get(2)));
			toReturn.add(n);
		}
		return toReturn;
	}
	
	static public DocumentXML getDocumentByID(int id) {
		String req = "SELECT D.id, D.nom " +
					 "FROM Documents D " +
					 "WHERE D.id =" + id + ";";

		List<List> result = bdd.getResultSet (bdd.executeSelect(req));
		DocumentXML d = new DocumentXML(Integer.parseInt((String) result.get(0).get(0)), (String)result.get(0).get(1));
		return d;
	}
	
	static public int insertDocument(String nom)
	{
		String req = "INSERT VALUES( '"+ nom + "') " +
		      	 	 "INTO Documents( 'nom'); " ;

		bdd.executeInsert(req);
		
		String req2 = "SELECT max(D.id) " +
		 			  "FROM Documents D ;";

		List<List> result = bdd.getResultSet (bdd.executeSelect(req));

		return Integer.parseInt((String)result.get(0).get(0));
	}
	
}
